package com.laundry.config;

import com.laundry.entity.*;
import com.laundry.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalTime;

@Component
@RequiredArgsConstructor
public class DataInitializer implements CommandLineRunner {
    
    private final UserRepository userRepository;
    private final ServiceCategoryRepository categoryRepository;
    private final MerchantRepository merchantRepository;
    private final ServiceRepository serviceRepository;
    private final PasswordEncoder passwordEncoder;
    
    @Override
    public void run(String... args) throws Exception {
        initializeData();
    }
    
    private void initializeData() {
        // 创建测试用户
        if (userRepository.count() == 0) {
            User testUser = new User();
            testUser.setUsername("testuser");
            testUser.setEmail("test@example.com");
            testUser.setPassword(passwordEncoder.encode("123456"));
            testUser.setMobile("13800138000");
            testUser.setNickname("测试用户");
            testUser.setStatus(User.UserStatus.ACTIVE);
            testUser.setRole(User.UserRole.USER);
            userRepository.save(testUser);
        }
        
        // 创建服务分类
        if (categoryRepository.count() == 0) {
            ServiceCategory[] categories = {
                createCategory("衣物洗护", "专业衣物清洗护理服务", "👔", 1),
                createCategory("鞋靴护理", "专业鞋靴清洗保养服务", "👟", 2),
                createCategory("家纺清洗", "床品窗帘等家纺用品清洗", "🛏️", 3),
                createCategory("皮具护理", "皮包皮衣等皮具专业护理", "👜", 4),
                createCategory("奢侈品护理", "高端奢侈品专业护理服务", "💎", 5)
            };
            
            for (ServiceCategory category : categories) {
                categoryRepository.save(category);
            }
        }
        
        // 创建商家
        if (merchantRepository.count() == 0) {
            Merchant[] merchants = {
                createMerchant("洁净洗衣店", "专业洗衣30年，值得信赖", "北京市朝阳区建国路88号"),
                createMerchant("优质干洗店", "高端干洗服务，呵护您的每一件衣物", "北京市海淀区中关村大街100号"),
                createMerchant("快速洗护中心", "24小时快速洗护，随时为您服务", "北京市西城区西单大街50号")
            };
            
            for (Merchant merchant : merchants) {
                merchantRepository.save(merchant);
            }
        }
        
        // 创建服务
        if (serviceRepository.count() == 0) {
            ServiceCategory clothingCategory = categoryRepository.findActiveCategories().get(0);
            ServiceCategory shoeCategory = categoryRepository.findActiveCategories().get(1);
            Merchant merchant = merchantRepository.findAll().get(0);
            
            Service[] services = {
                createService("普通衣物清洗", "日常衣物专业清洗", clothingCategory, merchant, new BigDecimal("15.00")),
                createService("高档衣物干洗", "高档面料专业干洗护理", clothingCategory, merchant, new BigDecimal("35.00")),
                createService("运动鞋清洗", "专业运动鞋深度清洗", shoeCategory, merchant, new BigDecimal("25.00")),
                createService("皮鞋护理", "皮鞋清洗保养护理", shoeCategory, merchant, new BigDecimal("30.00"))
            };
            
            for (Service service : services) {
                serviceRepository.save(service);
            }
        }
    }
    
    private ServiceCategory createCategory(String name, String description, String icon, int sortOrder) {
        ServiceCategory category = new ServiceCategory();
        category.setName(name);
        category.setDescription(description);
        category.setIcon(icon);
        category.setSortOrder(sortOrder);
        category.setStatus(ServiceCategory.CategoryStatus.ACTIVE);
        return category;
    }
    
    private Merchant createMerchant(String name, String description, String address) {
        Merchant merchant = new Merchant();
        merchant.setName(name);
        merchant.setDescription(description);
        merchant.setAddress(address);
        merchant.setPhone("400-123-4567");
        merchant.setEmail("service@" + name.toLowerCase().replaceAll("\\s+", "") + ".com");
        merchant.setOpenTime(LocalTime.of(8, 0));
        merchant.setCloseTime(LocalTime.of(22, 0));
        merchant.setIsOpen(true);
        merchant.setStatus(Merchant.MerchantStatus.ACTIVE);
        merchant.setRating(new BigDecimal("4.5"));
        merchant.setReviewCount(100);
        merchant.setOrderCount(500);
        merchant.setDeliveryFee(new BigDecimal("5.00"));
        merchant.setMinOrderAmount(new BigDecimal("20.00"));
        return merchant;
    }
    
    private Service createService(String name, String description, ServiceCategory category, 
                                Merchant merchant, BigDecimal price) {
        Service service = new Service();
        service.setName(name);
        service.setDescription(description);
        service.setCategory(category);
        service.setMerchant(merchant);
        service.setPrice(price);
        service.setOriginalPrice(price.multiply(new BigDecimal("1.2")));
        service.setPriceUnit(Service.PriceUnit.PIECE);
        service.setStatus(Service.ServiceStatus.ACTIVE);
        service.setProcessingTime(24);
        service.setRating(new BigDecimal("4.3"));
        service.setReviewCount(50);
        service.setOrderCount(200);
        service.setIsRecommended(true);
        service.setIsHot(false);
        return service;
    }
}
